package axdoc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import axdoc.model.RuleSelectValue;
import axdoc.rule.ActivityRulesUtil;
import axdoc.rule.check.Check;
import axdoc.rule.check.LoadChecksUtil;
import axdoc.service.CommonActivityService;

@Controller
@RequestMapping("rule")
public class RuleListController {

	@Autowired
	ActivityRulesUtil activityRulesUtil;
	@Autowired
	CommonActivityService commonActivityService;

	@RequestMapping(value = "list/{acid}/{type}")
	public ModelAndView rules(@PathVariable("acid") int acid, @PathVariable("type") int type, ModelAndView model) {
		if (type == 1000) {
			model.addObject("rules", activityRulesUtil.getConsultOnline());
		} else if (type == 1100) {
			model.addObject("rules", activityRulesUtil.getQuestionRules());
		}
		model.addObject("action", "addRules");
		model.addObject("businessType", type);
		model.addObject("acid", acid);
		model.setViewName("rule");
		return model;
	}

	@RequestMapping(value = "addRules")
	@ResponseBody
	public String addRules(RuleSelectValue ruleSelectValue) {

		List<Check> list = LoadChecksUtil.toCheckList(ruleSelectValue);

		int res = commonActivityService.addActRules(JSON.toJSONString(list), ruleSelectValue.getAcid());
		return "添加成功-返回数据-consult：" + JSON.toJSONString(ruleSelectValue) + " | res : " + res;
	}

}
